/*******************************************************************************
 *   Copyright 2007-2008 SIP Response
 *   Copyright 2007-2008 Michael D. Cohen
 *
 *      mike _AT_ sipresponse.com
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 ******************************************************************************/
package com.sipresponse.flibblecallmgr.internal.actions;


import com.sipresponse.flibblecallmgr.CallManager;
import com.sipresponse.flibblecallmgr.MediaSourceType;
import com.sipresponse.flibblecallmgr.internal.Call;
import com.sipresponse.flibblecallmgr.internal.FlibbleSipProvider;
import com.sipresponse.flibblecallmgr.internal.InternalCallManager;
import com.sipresponse.flibblecallmgr.internal.media.FlibbleMediaProvider;

public class ChangeMediaAction extends ActionThread
{
    private int timeout = 60000;

    private MediaSourceType mediaSourceType;

    private String mediaFilename;

    private FlibbleSipProvider flibbleProvider;

    private FlibbleMediaProvider mediaProvider;

    private boolean loop;

    public ChangeMediaAction(CallManager callMgr, Call call,
            MediaSourceType mediaSourceType, String mediaFilename, boolean loop)
    {
        super(callMgr, call, null);
        this.mediaSourceType = mediaSourceType;
        this.mediaFilename = mediaFilename;
        this.loop = loop;
        flibbleProvider = InternalCallManager.getInstance()
                .getProvider(callMgr);
    }

    public int getTimeout()
    {
        return timeout;
    }

    public void setTimeout(int timeout)
    {
        this.timeout = timeout;
    }

    public void run()
    {
        try
        {
            changeMediaSource();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void changeMediaSource()
    {

        mediaProvider = call.getMediaProvider();
        if (null != mediaProvider)
        {
            mediaProvider.changeMediaSource(this.mediaSourceType,
                    this.mediaFilename,
                    this.loop);
        }
    }

}
